package io.renren.modules.quickService.controller;

import com.alibaba.fastjson.JSON;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.quickService.entity.UserExtraEntity;
import io.renren.modules.quickService.entity.UserRightEntity;
import io.renren.modules.quickService.form.UserExtraAuditForm;
import io.renren.modules.quickService.service.AreacodeService;
import io.renren.modules.quickService.service.UserExtraService;
import io.renren.modules.quickService.service.UserRightService;
import io.renren.modules.quickService.vo.OrderTakerVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 用户基本信息
 *
 * @author zenger380
 * @email zenger380@163.com
 * @date 2018-09-26 15:53:08
 */
@RestController
@RequestMapping("quickService/userExtra")
public class UserExtraController {
    @Autowired
    private UserExtraService userExtraService;
    @Autowired
    private AreacodeService areacodeService;
    @Autowired
    private UserRightService userRightService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("quickService:userExtra:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userExtraService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 根据行政区划获取用户列表（不分页）
     */
    @RequestMapping("/takerList")
    @RequiresPermissions("quickService:userExtra:takerList")
    public R takerList(@RequestParam String adminCode) {
        List<OrderTakerVO> takerList = userExtraService.takerList(adminCode);

        return R.ok().put("takerList", takerList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userExtraId}")
    @RequiresPermissions("quickService:userExtra:info")
    public R info(@PathVariable("userExtraId") Long userExtraId) {
        UserExtraEntity userExtra = userExtraService.selectById(userExtraId);
        String[] adminCodes = new String[3];
        if (userExtra.getAdminCode() != null) {
            adminCodes = areacodeService.getCascadeAreaByLastAdminCode(userExtra.getAdminCode());
        }
        List<UserRightEntity> rightList = userRightService.getRightList(userExtra.getUserId());

        return R.ok().put("userExtra", userExtra).put("adminCodes", adminCodes).put("rightList", rightList);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("quickService:userExtra:save")
    public R save(@RequestBody UserExtraEntity userExtra) {
        userExtra.setAddress(areacodeService.getAddressByLastAdminCode(userExtra.getAdminCode()));
        Date updateTime = new Date();
        userExtra.setCreateTime(updateTime);
        userExtra.setUpdateTime(updateTime);
        userExtraService.insert(userExtra);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("quickService:userExtra:update")
    public R update(@RequestBody UserExtraEntity userExtra) {
        userExtra.setAddress(areacodeService.getAddressByLastAdminCode(userExtra.getAdminCode()));
        userExtraService.updateById(userExtra);
        String rightList = userExtra.getRightList();
        List<UserRightEntity> rightEntities = JSON.parseArray(rightList, UserRightEntity.class);
        if (rightEntities != null) {
            for (UserRightEntity rightEntity : rightEntities) {
                rightEntity.setUserId(userExtra.getUserId());
                rightEntity.setRightNo(rightEntity.getRightStatus());
                if (rightEntity.getCreateTime() == null) {
                    rightEntity.setCreateTime(new Date());
                }
                userRightService.insertOrUpdateUserRight(rightEntity);
            }
        }
        return R.ok();
    }

    /**
     * 审核
     */
    @RequestMapping("/audit")
    @RequiresPermissions("quickService:userExtra:audit")
    public R update(@RequestBody UserExtraAuditForm userExtraAuditForm) {
        userExtraService.updateById(userExtraAuditForm.getUserExtra());
        userRightService.insertOrUpdateUserRight(userExtraAuditForm.getUserExtra().getUserId(), userExtraAuditForm.getRightList());
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("quickService:userExtra:delete")
    public R delete(@RequestBody Long[] userExtraIds) {
        userExtraService.deleteBatchIds(Arrays.asList(userExtraIds));

        return R.ok();
    }

}
